package cs2340.quarterblack.wheresmystuff;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputType;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Spinner;

public class FoundItem extends Activity {
	
	Button btnAdd;
	ListView items;
	Account account;
	ArrayList<Item> list;
	EditText text;
	Update update;
	boolean hasItems = true;
	ArrayAdapter<Item> adapter;
	Spinner spinner;

	/**
	 * creates the main activity page
	 */
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_losttab);
		// Show the Up button in the action bar.
		account = Model.getAccount();
		btnAdd = (Button)findViewById(R.id.lt_addItem);
		items = (ListView)findViewById(R.id.lt_list);
		text = (EditText)findViewById(R.id.lt_search);
		spinner = (Spinner)findViewById(R.id.lt_spinner);
		items.setOnItemClickListener(new OnItemClickListener()
		{
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
			if(hasItems == true)
			{
				Intent itemPage = new Intent(FoundItem.this,ItemPage.class);
				Model.setItem(list.get(arg2));
				FoundItem.this.startActivity(itemPage);		
			}
		}
			
		});
			list = new ArrayList<Item>();
			if(account.getFoundItems() != null)
			{
				list.clear();
				list.addAll(account.getFoundItems());
			}
			if(list.size() == 0)
			{
				list.add(new Item(null,"No Items to Display"));
				hasItems = false;
			}
			adapter= new ArrayAdapter<Item>(this, android.R.layout.simple_list_item_1,list);
			items.setAdapter(adapter);
			text.addTextChangedListener(new Update(text,adapter));
			
			ArrayAdapter<String> sadapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,Account.getSortList());
			spinner.setAdapter(sadapter);
			spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

	            public void onItemSelected(AdapterView<?> arg0, View arg1,
	                    int arg2, long arg3) {
	            	changeList(text.getText().toString());
	            }

	            public void onNothingSelected(AdapterView<?> arg0) {
	               

	            }
	        });

	    /**
		 * listens for the add button press
		 */
		btnAdd.setOnClickListener(new OnClickListener(){
			
			@Override
			public void onClick(View v) {
				Intent createItemIntent = new Intent(FoundItem.this, CreateItem.class);
				createItemIntent.putExtra("state", true);
				FoundItem.this.startActivity(createItemIntent);
			}
		});
	}
	/**
	 * class for watching text field and responses when when text changes
	 */
	private class Update implements TextWatcher
	{
		EditText text;
		ArrayAdapter<Item> adapter;
		Update(EditText text, ArrayAdapter<Item> adapter)
		{
			this.text = text;
			this.adapter = adapter;
		}
		@Override
		public void afterTextChanged(Editable arg0) {
			changeList(text.getText().toString());
			adapter.notifyDataSetChanged();
			
		}
		@Override
		public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
				int arg3) {
			
		}
		@Override
		public void onTextChanged(CharSequence arg0, int arg1, int arg2,
				int arg3) {
			adapter.notifyDataSetChanged();
			
			
		}
		
	}
	/**
	 * modifies the list depending on which category is selected and string passed in
	 * @param str
	 */
	private void changeList(String str)
	{
		list.clear();
		if(spinner.getSelectedItemPosition() == 0)
		{
			text.setInputType(InputType.TYPE_CLASS_TEXT);
			Model.setItemRep(0);
			list.clear();
			list.addAll(account.getFoundItemsName(str));
			adapter.notifyDataSetChanged();
		}
		else if(spinner.getSelectedItemPosition() == 1)
		{
			Model.setItemRep(1);
			text.setInputType(InputType.TYPE_CLASS_TEXT);
			if(text.getText().toString() == null || text.getText().toString().length() == 0)
			{
				list.addAll(account.getFoundItemsCategory());
			}
			else
			{
				list.addAll(account.getFoundItemsCategory(text.getText().toString()));
			}
			adapter.notifyDataSetChanged();
		}
		else if(spinner.getSelectedItemPosition() == 2)
		{
			Model.setItemRep(2);
			text.setInputType(InputType.TYPE_DATETIME_VARIATION_DATE);		
			list.addAll(account.getFoundItemsDate());
			adapter.notifyDataSetChanged();
		}

	}
}
